//
//  GFIntroViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFIntroViewController.h"

@interface GFIntroViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, GFIntroCellDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flow;
@property (nonatomic, strong) UIView *bigView;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, copy) NSString *pageCount;

@end

@implementation GFIntroViewController

- (void)viewWillAppear:(BOOL)animated {
    
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    
    view.backgroundColor = [UIColor clearColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

}

#pragma mark _ 数据处理
- (void)handleData {
    
    NSString *str = @"http://food.boohee.com/fb/v1/topics";
    
    NSString *urlStr = [str stringByAppendingFormat:@"/%@", self.idNumber];
    [AFNetHandle GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [NSArray arrayWithArray:[[result objectForKey:@"topic"] objectForKey:@"pages"]];
        
        
        self.pageCount = [[result objectForKey:@"topic"] objectForKey:@"page_count"];
        
        self.contentArr = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
            self.introModel = [GFIntroModel modWithDic:dic];
            
            
            [self.contentArr addObject:self.introModel];
            
            
            
        }
        
        [self.collectionView reloadData];
        [self.overTimeView removeFromSuperview];
        [self.waitView removeFromSuperview];
        
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
        [self.view bringSubviewToFront:self.bigView];
        
    }];
    
    //    NSLog(@"%@", urlStr);
}


#pragma mark - 创建collectionView
- (void)creatCollectionView {
    
    self.flow = [[UICollectionViewFlowLayout alloc] init];
    self.flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.flow.minimumInteritemSpacing = 0;
    self.flow.minimumLineSpacing = 0;
    self.flow.itemSize = CGSizeMake(ScreenWidth, ScreenHeight);
    self.flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:self.flow];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[GFIntroCell class] forCellWithReuseIdentifier:@"IntroCollectionViewCell"];
    [self.view addSubview:self.collectionView];

    
}

#pragma mark _ collectionView DataSource 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.pageCount.integerValue;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GFIntroCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IntroCollectionViewCell" forIndexPath:indexPath];
    
    
    GFIntroModel *introModel = [self.contentArr objectAtIndex:indexPath.item];
    
    
    cell.introModel = introModel;
    
    cell.delegate = self;
    
    [cell useLayoutSubViews];
    
    return cell;
}

- (void)sendMethodCode:(NSString *)code {
    
    GFDetialsViewController *detailsVC = [[GFDetialsViewController alloc] init];
    
    
    detailsVC.food_code = code;
    
    
    [self.navigationController pushViewController:detailsVC animated:YES];

}
#pragma mark - 延迟加载
- (void)delayLoad {
    
    [self creatCollectionView];
//        [self.view bringSubviewToFront:self.waitView];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self.view bringSubviewToFront:self.bigView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.waitView removeFromSuperview];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"houtui1"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLastVC:)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(shareCurrentPage:)];
    self.navigationItem.rightBarButtonItem = shareItem;
    self.navigationController.navigationBarHidden = YES;
    [self performSelector:@selector(delayLoad) withObject:nil afterDelay:1.1];
   
    self.bigView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.imageView = [[UIImageView alloc] initWithFrame:self.bigView.frame];
    self.imageView.backgroundColor = [UIColor whiteColor];
    [self.imageView setImageWithURL:[NSURL URLWithString:self.imageUrl]];
    [self.bigView addSubview:self.imageView];
    
    [self transAnimation];
    [self.view addSubview:self.bigView];
    
    [self performSelector:@selector(delay) withObject:nil  afterDelay:2.5];
    [self performSelector:@selector(remove) withObject:nil afterDelay:3.3];
   

}
- (void)remove {
    [self.bigView removeFromSuperview];
    
}
#pragma mark - 延迟
- (void)delay {
    [self BasicAnimation];
    
    [self handleData];
}
#pragma mark - basic动画
- (void)BasicAnimation {
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.fromValue = [NSNumber numberWithInt:1];
    basic.toValue = [NSNumber numberWithInt:0];
    basic.duration = 1;
    basic.autoreverses = 0;
    basic.repeatCount = 0;
    [self.imageView.layer addAnimation:basic forKey:@"basic"];
    self.navigationController.navigationBarHidden = NO;
    //    [self.bigView removeFromSuperview];
}
#pragma mark - trans动画
- (void)transAnimation {
    CATransition *trans = [CATransition animation];
    trans.type = kCATransitionReveal;
    trans.duration = 1;
    trans.autoreverses = 0;
    trans.repeatCount = 0;
    [self.bigView.layer addAnimation:trans forKey:@"trans"];
}

- (void)shareCurrentPage:(UIBarButtonItem *)item {
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55d42591e0f55a92f5002e74"
                                      shareText:nil
                                     shareImage:nil                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]                                           delegate:nil];
    
}

- (void)backToLastVC:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
